/* loading全局插件 */
import Loading from '@/components/loading';
// https://zhuanlan.zhihu.com/p/26057542
export default {
  /**
   * 每个插件都有的install方法，用于安装插件
   * @param {Object} Vue - Vue类
   * @param {Object} [pluginOptions] - 插件安装配置
   */
  install(Vue, pluginOptions = {}) {
    console.log(pluginOptions);
    // 创建"子类"方便挂载
    const VueLoading = Vue.extend(Loading);
    let loading = null;
    function $loading() {
      return new Promise((resolve) => {
          // 第一次调用
        if (!loading) {
          loading = new VueLoading();
            // 手动创建一个未挂载的实例
          loading.$mount();
            // 挂载
            // this.$refs.input1 减少获取dom节点的消耗
          document.querySelector(pluginOptions.container || 'body').appendChild(loading.$el);
        }
        // 显示loading
        // loading.show();
        resolve();
      });
    }
    $loading.end = (noAnimate = false) => {
      console.log(noAnimate);
      return new Promise((resolve) => {
        console.log(resolve);
        if (!loading || !loading.isShow) {
          resolve();
        }
        if (noAnimate) {
          loading.removeAnimate().then(() => {
            loading.opemAnimate();
          });
        }
        loading.hide();
      });
    };
    Vue.prototype.$loading = $loading;
    Vue.loading = Vue.prototype.$loading;
    // Vue.loading = Vue.prototype.$loading = $loading;
  },
};
